<template>
 <el-container>
   <!-- header -->
  <el-header>
    <div class="logo-left">
      <img src="../assets/logo.jpg" alt="" srcset="" class="logo">
      <span>电商管理系统</span>
    </div>
    <el-button @click="loginOutFn">退出</el-button>
  </el-header>
  <!-- 内容区 -->
  <el-container>
    <!-- 左边 侧边栏 -->
    <el-aside :width="isCollapse ? '65px' : '200px'">
      <div class="collapse">
        <i :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'" @click="collapseFn"></i>
      </div>
      <el-menu
        class="el-menu-vertical-demo"
        router
        :default-active="$route.path"
        unique-opened
        :collapse="isCollapse"
        :collapse-transition= "false"
        background-color="#323744"
        text-color="#fff"
        active-text-color="#349bff">
        <el-submenu :index=" '/' + item.path" v-for="item in menuList" :key="item.id">
          <template slot="title">
            <i :class="iconObj[item.id]"></i>
            <span>{{item.authName}}</span>
          </template>
          <el-menu-item :index=" '/' + subItem.path" v-for="subItem in item.children" :key="subItem.id">
            <template slot="title">
              <i class="el-icon-setting"></i>
              <span>{{subItem.authName}}</span>
            </template>
          </el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
    <!-- 右边 -->
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
// @ is an alias to /src

export default {
  name: 'Home',
  mounted () {
    this.getAsideMenu()
  },
  data () {
    return {
      isCollapse: false,
      menuList: [],
      iconObj: {
        125: 'el-icon-user',
        103: 'el-icon-notebook-1',
        101: 'el-icon-shopping-cart-full',
        102: 'el-icon-document',
        145: 'el-icon-pie-chart'
      }
    }
  },
  methods: {
    // 退出登录
    loginOutFn () {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    // 获取左侧菜单栏
    async getAsideMenu () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) this.$message.error(res.meta.msg)
      this.menuList = res.data
      console.log(res)
    },
    // 折叠
    collapseFn () {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>

<style lang="less" scoped>
  .el-container {
    height: 100%;
    .el-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      background-color: #373c41;
      color: #fff;
      .logo-left {
        display: flex;
        align-items: center;
        height: 100%;
        font-size: 20px;
        .logo {
          height: 80%;
          border-radius: 4px;
          margin-right: 16px;
        }
      }
    }
    .el-aside {
      background-color: #323744;
      position: relative;
      .el-menu {
        border-right: none;
      }
      .collapse {
        position: absolute;
        // width: 100%;
        right: 10px;
        bottom: 60px;
        height: 40px;
        color: #fff;
        font-size: 30px;
        cursor: pointer;
      }
    }
    .el-main {
      background-color: #eaedf2;
    }
  }
</style>
